<template>
	<el-dialog title="密码重置" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		
			<el-form ref="form" :model="form" :rules="rules" label-width="120px">
				<el-form-item label="新密码" prop="newPassword">
					<el-input v-model="form.newPassword" type="password" show-password placeholder="请输入新密码" @input="passChange"></el-input>
					<sc-password-strength ref="passwordStrength" v-model="form.newPassword"></sc-password-strength>
					<div class="el-form-item-msg">请输入包含字母、数字的8位以上密码</div>
				</el-form-item>
				<el-form-item label="确认新密码" prop="confirmNewPassword">
					<el-input v-model="form.confirmNewPassword" type="password" show-password placeholder="请再次输入新密码"></el-input>
				</el-form-item>
				<!-- <el-form-item label="密码过期时间" prop="passwordExpired">
					<el-date-picker v-model="form.passwordExpired" type="datetime" placeholder="选择时间" 
					format="YYYY-MM-DD" value-format="YYYY-MM-DD"  @change="timeChange"></el-date-picker>
				</el-form-item> -->
				<el-form-item label="" prop="passwordNext">
					<el-checkbox v-model="form.passwordNext" :true-label="1" :false-label="0" :checked="form.passwordNext"></el-checkbox> 
					&nbsp;下次登录要求修改密码
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="save">重置</el-button>
					<el-button @click="passwordGeneratorVisible = true">生成密码</el-button> 
				</el-form-item>
			</el-form>

			<!-- ="this.$refs.passwordStrength.level < 2" -->
			<password-generator v-if="passwordGeneratorVisible" @success="handleGenPassword" @closed="passwordGeneratorVisible=false"></password-generator>

	</el-dialog>
	
	
</template>

<script>
	import PasswordGenerator from '@/components/ncPasswordGenerator'
	import scPasswordStrength from '@/components/scPasswordStrength8'
	import setting from './setting'
	import JSEncrypt from 'jsencrypt'
	export default {
		components: {
			scPasswordStrength,
			setting,
			PasswordGenerator
		},
		data() {
			return {
				passwordGeneratorVisible: false,
				visible: false,
				form: {
					newPassword: "",
					confirmNewPassword: "",
					// passwordExpired: "",
					passwordNext: 0
				},
				rules: {
					newPassword: [
						{ required: true, message: '请输入新密码'},
						{validator: (rule, value, callback) => {
							this.$refs.passwordStrength.strength(value)
							let passLevel = this.$refs.passwordStrength.level
							if (passLevel < 2) {
								callback(new Error('密码复杂度不足'));
							}else{
								callback();
							}
						}}
					],
					confirmNewPassword: [
						{ required: true, message: '请再次输入新密码'},
						{ validator: (rule, value, callback) => {
							if (value !== this.form.newPassword) {
								callback(new Error('两次输入密码不一致'));
							}else{
								callback();
							}
						}}
					]
				},
				publicKey: null
			}
		},
		async mounted () {
			//导入 public key
			let pubk = await this.$API.system.pub.publicKey.get();
			this.publicKey = (pubk.code == 0) ? pubk.data : null;
		},
		methods: {
			open(){
				this.visible = true;
				return this
			},
			save(){

				this.$refs.form.validate(async valid => {
					if (valid) {
		
						let d = {
							id : this.form.id,
							password: this.form.newPassword,
							isNextModify: this.form.passwordNext
						}

						//
						if (this.publicKey) {							
							let encrypt = new JSEncrypt();
							encrypt.setPublicKey(this.publicKey)
							d.password = encrypt.encrypt(d.password)
						}

						// if (this.form.passwordExpired) {
						// 	d.expireTime= this.form.passwordExpired
						// }

						var res = await setting.api.passwordReset.post(d)
						if(res.code == setting.successCode){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("重置成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}

					}else{
						return false
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.passwordNext = data.passwordNext
				// this.form.passwordExpired = data.passwordExpired

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			},
			timeChange(date){
				this.form.passwordExpired = date + "T00:00:00+0000"
			},
			handleGenPassword(p) {
				if (p) {
					this.form.newPassword = p
					this.form.confirmNewPassword = p
				}
			}
		}
	}
</script>

<style>
</style>
